package bandidong.dao.impl;

import bandidong.dao.CompanyDAO;
import bandidong.model.Company;
import bandidong.utils.Constant;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

public class CompanyDAOImpl implements CompanyDAO {
    
    private EntityManager em;
    
    public CompanyDAOImpl(EntityManager em) {
        this.em = em;
    }
    
    @Override
    public List<Company> getAllCompanies() {
        try {
            String jpaql = "SELECT c FROM Company c ORDER BY c.companyName";
            Query query = em.createQuery(jpaql);
            List<Company> companies = (List<Company>) query.getResultList();
            return companies;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    public Company getCompanyById(int companyId) {
        try {
            String jpaql = "SELECT c FROM Company c WHERE c.companyId = :companyId";
            Query query = em.createQuery(jpaql);
            query.setParameter("companyId", companyId);
            List<Company> companies = (List<Company>) query.getResultList();
            if (!companies.isEmpty()) {
                return companies.get(0);
            }
            return null;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    public List<Company> getAllCompanies(int page, int pageSize) {
        if (page <= 0 || pageSize <= 0) {
            return null;
        }
        try {
            String jpaql = "SELECT c FROM Company c";
            Query query = em.createQuery(jpaql);
            query.setFirstResult((page - 1) * pageSize);
            query.setMaxResults(pageSize);
            List<Company> companies = (List<Company>) query.getResultList();
            return companies;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    public boolean checkExists(String companyName, int companyId) {
        try {
            String jpaql = "SELECT c FROM Company c WHERE c.companyName = :companyName AND c.companyId = :companyId";
            Query query = em.createQuery(jpaql);
            List<Company> companies = (List<Company>) query.getResultList();
            if (companies.isEmpty()) {
                return false;
            }
            return true;
        }
        catch (Exception ex) {
            return true;
        }
    }

    @Override
    public String addCompany(Company company) {
        try {
            em.persist(company);
            return Constant.SUCCESS;
        }
        catch (Exception ex) {
            return Constant.ERROR;
        }
    }

    @Override
    public String updateCompany(Company company) {
        try {
            Company c = em.find(Company.class, company.getCompanyId());
            if (c != null) {
                c.setCompanyName(company.getCompanyName());
                em.merge(c);
                return Constant.SUCCESS;
            }
            return Constant.ERROR;
        }
        catch (Exception ex) {
            return Constant.ERROR;
        }
    }

    @Override
    public String deleteCompany(Company company) {
        try {
            Company c = em.find(Company.class, company.getCompanyId());
            em.remove(c);
            return Constant.SUCCESS;
        }
        catch (Exception ex) {
            return Constant.ERROR;
        }
    }

    @Override
    public Company getCompanyByName(String companyName) {
        try {
            String jpaql = "SELECT c FROM Company c WHERE c.companyName = :companyName";
            Query query = em.createQuery(jpaql);
            query.setParameter("companyName", companyName);
            List<Company> companies = (List<Company>) query.getResultList();
            if (!companies.isEmpty()) {
                return companies.get(0);
            }
            return null;
        }
        catch (Exception ex) {
            return null;
        }
    }
}
